package cn.hxzy.icon.icon.controller;

import cn.hxzy.icon.icon.entity.IconGroup;
import cn.hxzy.icon.icon.entity.Resource;
import cn.hxzy.icon.icon.entity.Role;
import cn.hxzy.icon.icon.entity.po.IconGroupPo;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.IconGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("group")
public class IconGroupController {
    @Autowired
    private IconGroupService iconGroupService;

    /**
     * 查询全部
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size) {
        PageBean<IconGroup> all = iconGroupService.findAll(new PageBean<IconGroup>(page, size));
        System.out.println(all);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 查询全部
     * @return
     */
    @PostMapping("findAll")
    public ReturnBean findAll() {
        List<IconGroup> all =iconGroupService.findAll1();
        System.out.println(all);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean delete(Integer id) {
        iconGroupService.deleteById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * id查询信息
     *
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        IconGroup iconGroup = iconGroupService.FindById(id);
        System.out.println(iconGroup);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, iconGroup);
    }

    /**
     * 修改
     * @param iconGroupPo
     * @return
     */
    @PostMapping("update")
    public ReturnBean update(IconGroupPo iconGroupPo) {
        IconGroup iconGroup = new IconGroup();
        BeanUtils.copyProperties(iconGroupPo, iconGroup);
        iconGroupService.updeat(iconGroup);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 添加
     *
     * @param iconGroup
     * @return
     */
    @PostMapping("add")
    public ReturnBean add(IconGroup iconGroup) {
        if (iconGroup != null) {
            iconGroupService.add(iconGroup);
            return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.ADD_ERROR);
    }
}
